package ihm;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JDialog;
import javax.swing.JTextField;
import javax.swing.JList;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;

import metier.Collaborateur;
import metier.Contrat;

import java.awt.Component;
import java.awt.Dialog.ModalExclusionType;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

@SuppressWarnings("serial")
public class FenetreAjouterTache extends JDialog{
	private JTextField txtLibelleTache;
	private JTextField txtChargeTache;
	private JTextField txtNumTache;
	private JComboBox cboContrat;
	private JComboBox cboCollaborateur;
	private ArrayList<Collaborateur> lesCollaborateurs;
	private ArrayList<Contrat> lesContrats;

	/**
	 * Create the application.
	 */
	public FenetreAjouterTache() {
		setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
		setModal(true);
		initialize();
		remplirCboCollaborateur();
		remplirCboContrat();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		this.setTitle("Ajouter une tâche");
		this.setBounds(100, 100, 450, 300);
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		
		JLabel lblNumroDeTche = new JLabel("Numéro de Tâche :");
		
		JLabel lblLibell = new JLabel("Libellé :");
		
		JLabel lblChargePrvisionnelle = new JLabel("Charge prévisionnelle :");
		
		JLabel lblCollaborateur = new JLabel("Collaborateur :");
		
		JLabel lblContrat = new JLabel("Contrat :");
		
		txtLibelleTache = new JTextField();
		txtLibelleTache.setColumns(10);
		
		txtChargeTache = new JTextField();
		txtChargeTache.setColumns(10);
		
		JList list = new JList();
		
		cboCollaborateur = new JComboBox();
		
		cboContrat = new JComboBox();
		
		JButton btnValider = new JButton("Valider");
		
		JButton btnAnnuler = new JButton("Annuler");
		btnAnnuler.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		
		txtNumTache = new JTextField();
		txtNumTache.setColumns(10);
		GroupLayout groupLayout = new GroupLayout(this.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(64)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(48)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(btnValider, GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(btnAnnuler, GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE))
								.addComponent(list, GroupLayout.PREFERRED_SIZE, 1, GroupLayout.PREFERRED_SIZE))
							.addGap(45))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNumroDeTche)
								.addComponent(lblLibell)
								.addComponent(lblChargePrvisionnelle)
								.addComponent(lblCollaborateur)
								.addComponent(lblContrat))
							.addGap(23)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(cboContrat, 0, 64, Short.MAX_VALUE)
								.addComponent(txtNumTache)
								.addComponent(cboCollaborateur, 0, 105, Short.MAX_VALUE)
								.addComponent(txtLibelleTache, GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE)
								.addComponent(txtChargeTache, GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE))))
					.addGap(117))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(33)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(lblNumroDeTche)
						.addComponent(txtNumTache, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblLibell)
						.addComponent(txtLibelleTache, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblChargePrvisionnelle)
						.addComponent(txtChargeTache, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblCollaborateur)
						.addComponent(cboCollaborateur, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblContrat)
						.addComponent(cboContrat, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(2)
					.addComponent(list, GroupLayout.PREFERRED_SIZE, 1, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnValider)
						.addComponent(btnAnnuler))
					.addContainerGap(20, Short.MAX_VALUE))
		);
		this.getContentPane().setLayout(groupLayout);
	}
	
	@SuppressWarnings("unchecked")
	public void remplirCboCollaborateur() {
		lesCollaborateurs = FenetrePrincipale.getLesCollaborateurs();

		for (int i = 0; i < lesCollaborateurs.size(); i++) {
			cboCollaborateur.addItem(lesCollaborateurs.get(i));
		}
	}

	@SuppressWarnings("unchecked")
	public void remplirCboContrat() {
		lesContrats = FenetrePrincipale.getLesContrats();

		for (int i = 0; i < lesContrats.size(); i++) {
			cboContrat.addItem(lesContrats.get(i));
		}
	}
}
